package com.bjc.core.controller;

import java.util.List;

import com.bjc.base.core.annotation.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.bean.PermissionTreeVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.core.annotation.PermissionPath;
import com.bjc.core.bean.request.PropertyReqVO;
import com.bjc.core.service.IPropertyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Property控制器
 * 
 * @author 许自辉
 * @date 2017年7月5日 下午5:23:15
 */
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/数据字典")
@Api(value = "property-api", tags = { "数据字典相关接口" })
public class PropertyController {

	@Autowired
	private IPropertyService propertyService;

	@ApiOperation("查询数据字典")
//	@ApiImplicitParams({
//		@ApiImplicitParam(name = "value", value = "值", required = false, dataType = "string", paramType = "path", defaultValue = "") 
//		})
	@Permission("property:find")
	@GetMapping("property/find")
	public ResultVO getProperty(PropertyReqVO propertyReqVO) {
		return ResultGenerator.getSuccess(propertyService.get(propertyReqVO));
	}
	
	@ApiOperation("查询数据字典树")
	@Permission("property:findTree")
	@GetMapping("property/findTree")
	public ResultVO getPropertyTree(PropertyReqVO propertyReqVO) {
		List<PermissionTreeVO> permissionTreeVOs = propertyService.getTree(propertyReqVO);
		return ResultGenerator.getSuccess(PermissionTreeVO.getTreeList(permissionTreeVOs));
	}

	@ApiOperation("添加数据字典")
	@Permission("property:create")
	@PostMapping("property/create")
	public ResultVO createProperty(@RequestBody @Validated PropertyReqVO propertyReqVO) {
		int count = propertyService.insert(propertyReqVO);
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新数据字典")
	@Permission("property:update")
	@PutMapping("property/update")
	public ResultVO updateProperty(@RequestBody @Validated PropertyReqVO propertyReqVO) {
		int count = propertyService.update(propertyReqVO);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("删除数据字典")
	@Permission("property:delete:id")
	@DeleteMapping("property/delete/{id}")
	public ResultVO deletePropertyById(@ApiParam(value = "数据字典id", required = true) @PathVariable("id") Integer id) {
		int count = propertyService.deleteById(id);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
